﻿using Pay.Dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pay.Model.Interfaces;
using Pay.Model.Model;
using Pay.Dapper;
using Dapper.Contrib.Extensions;
using Dapper;
using Pay.Model;

namespace Pay.DalImplement
{
    public class ApplyDalImplement : IApplyDal
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<long> Add(ApplyInfo model)
        {
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
               return await conn.InsertAsync<ApplyInfo>(model);
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="searchWord"></param>
        /// <param name="MId"></param>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="offset"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public async Task<IPagedOutput<ApplyInfo>> GetPage(long? MId, DateTime? timeBegin = null, DateTime? timeEnd = null, int offset = 10, int rows = 1)
        {
            StringBuilder sb = new StringBuilder("SELECT Id,MerchantId,ApplyAmount,Status,Des,CreateTime,MerName,BankCard from apply_info  where 1=1");
            DynamicParameters param = new DynamicParameters();
            //param.Add("userId", userId);
            if (timeBegin.HasValue && timeEnd.HasValue)
            {
                sb.Append(" and  createtime >=@timeBegin and createTime <=@timeEnd");
                param.Add("@timeBegin", timeBegin.Value);
                param.Add("@timeEnd", timeEnd.Value);

            }

            if (MId.HasValue)
            {
                sb.Append(" and merchantId=@mid");
                param.Add("@mid", MId.Value);
            }

            using (var conn = ConnectionFactory.GetReadConnection())
            {
                return await conn.PagedByAsync<ApplyInfo>(sb, param, offset, rows, "createTime", false);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(ApplyInfo model)
        {
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
                return await conn.UpdateAsync<ApplyInfo>(model);
            }
        }

        /// <summary>
        /// 更加商户ID获取商户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<MerchantInfo> GetByMerchantId(long id)
        {
            string sql = "select id,TotalAmount from merchant_info where id=@id";

            using (var conn = ConnectionFactory.GetWriteConnection())
            {
               return await conn.QueryFirstOrDefaultAsync<MerchantInfo>(sql, new { id = id });
            }
        }
        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApplyInfo> GetById(long id)
        {
            string sql = "select Id,MerchantId,ApplyAmount,Status,Des,CreateTime,MerName,BankCard from apply_info where id=@id";
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
                return await conn.QueryFirstOrDefaultAsync<ApplyInfo>(sql, new { id = id });
            }
        }
    }
}
